﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// ControlBind 的摘要说明
/// </summary>
/// 
namespace MyBind
{
    public class ControlBind
    {
        public ControlBind()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        /// <summary>
        /// treeBind将数据绑定到树
        /// <param name="tree" >要绑定的树控件</param>
        /// <param name="mid">父节点的id</param>
        /// </summary>
        ///
        public void treeBind(TreeView tree,int mid)
        {
            DAL.sysModule objModule = new DAL.sysModule();
            DataSet ds = objModule.GetList("mid=" + mid);
            DataTable dt = ds.Tables[0];
            //取出根节点信息，创造节点
            if (dt.Rows.Count > 0)
            {
                TreeNode nd = new TreeNode();
                nd.Text = dt.Rows[0]["mname"].ToString();
                nd.ImageUrl=@"~/Images/icon/base.gif";
                string url=dt.Rows[0]["pageUrl"].ToString();//该节点对应的页面路径

                if (url == null || url.Equals(""))
                {
                    nd.NavigateUrl = "javascript:void(0)";
                }
                else
                {
                    nd.NavigateUrl = url;
                    nd.Target = "main";
                }
                tree.Nodes.Add(nd);
                nd.Expand();
                //查询根节点下的子节点
                ds.Clear();
                ds = objModule.GetList("parentMId=" + mid);
                dt = ds.Tables[0];
                //传递数据过去，显示子节点
                this.getChild(nd, dt);
            }
        }

        //显示子节点的递归方法
        public void getChild(TreeNode nd, DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                //创建新的节点
                TreeNode child = new TreeNode();
                string mid = row["mid"].ToString();
                child.Text = row["mname"].ToString();
                string url = row["pageUrl"].ToString();
                if (url == null || url.Equals(""))
                {
                    child.NavigateUrl = "javascript:void(0)";
                }
                else
                {
                    child.NavigateUrl = url;
                    child.Target = "main";
                }
                //如果还有下级节点则设置对应的图标
                DAL.sysModule objModule = new DAL.sysModule();
                if (objModule.HasChild(int.Parse(mid)))
                {
                    child.ImageUrl = "~/Images/icon/folder.gif";
                }
                else
                {
                    child.ImageUrl = "~/Images/right.gif";
                }
                //在上级节点nd下添加child
                nd.ChildNodes.Add(child);
                nd.Expand();
                DataSet ds = objModule.GetList("parentMId=" + mid);
                DataTable dt2 = ds.Tables[0];
                getChild(child, dt2);//递归调用
            }
        }


    }
}